package com.wb.web.controller.exams;


import com.wb.common.core.controller.BaseController;
import com.wb.common.core.domain.AjaxResult;
import com.wb.common.core.page.TableDataInfo;
import com.wb.common.vo.*;
import com.wb.system.domain.ExamRecordsEntity;
import com.wb.system.domain.ExamsEntity;
import com.wb.system.domain.WrongQuestionsEntity;
import com.wb.system.service.ExamsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("exams")
@Api(tags = "考试流程")
public class ExamsController extends BaseController {
    @Autowired
    private ExamsService examsService;

    @PostMapping("/list")
    @ApiOperation("考试试卷列表")
    public TableDataInfo list(@RequestBody ExamsModelVO examsModelVO) {
        startPage();
        List<ExamsEntity> list = examsService.selectExamsList(examsModelVO);
        return getDataTable(list);
    }

    @PostMapping("/{id}/start")
    @ApiOperation("开始考试")
    public AjaxResult examsStart(@PathVariable("id") Long id) {
        return AjaxResult.success(examsService.examsStart(id));
    }

    @PostMapping("/{id}/submit")
    @ApiOperation("提交答案")
    public AjaxResult examsSubmit(@RequestBody ExamsSubmitVO vo) {
        return AjaxResult.success(examsService.examsSubmit(vo));
    }

    @GetMapping("/results/{id}")
    @ApiOperation("获取考试详情")
    public AjaxResult examsResults(@PathVariable("id") Long id) {
        return AjaxResult.success(examsService.examsResults(id));
    }

    @PostMapping("/history")
    @ApiOperation("获取用户考试历史")
    public TableDataInfo history(@RequestBody ExamsHistoryModelVO vo) {
        startPage();
        List<ExamRecordsEntity> list = examsService.history(vo);
        return getDataTable(list);
    }

    @PostMapping("/stats/question")
    @ApiOperation("获取当前考试成绩统计")
    public AjaxResult statsQuestion(@RequestBody ExamsStatsQuestionModelVO vo) {
        return AjaxResult.success(examsService.statsQuestion(vo));
    }

    @PostMapping("/stats")
    @ApiOperation("获取考试统计")
    public AjaxResult stats(@RequestBody ExamsStatsModelVO vo) {
        return AjaxResult.success(examsService.stats(vo));
    }

    @PostMapping("/type/stats")
    @ApiOperation("考试类型分布")
    public AjaxResult typeStats(@RequestBody ExamsStatsModelVO vo) {
        return AjaxResult.success(examsService.typeStats(vo));
    }

    @PostMapping("/wrong-questions")
    @ApiOperation("获取错题集")
    public TableDataInfo wrongQuestions(@RequestBody ExamsWrongQuestionsModelVO vo) {
        startPage();
        List<WrongQuestionsEntity> list = examsService.wrongQuestions(vo);
        return getDataTable(list);
    }


    @GetMapping("/{id}")
    @ApiOperation("获取考试试卷详情")
    public AjaxResult get(@PathVariable("id") Long id) {
        return AjaxResult.success(examsService.selectById(id));
    }

    @PostMapping("/add")
    @ApiOperation("考试试卷创建")
    public AjaxResult add(@Valid @RequestBody ExamsEntity ExamsModelVO) {
        ExamsEntity ExamsEntity = new ExamsEntity();
        BeanUtils.copyProperties(ExamsModelVO, ExamsEntity);
        examsService.saveExams(ExamsEntity);
        return AjaxResult.success();
    }


    @PostMapping("/update")
    @ApiOperation("更新考试试卷")
    public AjaxResult update(@RequestBody ExamsEntity ExamsModelVO) {
        ExamsEntity ExamsEntity = new ExamsEntity();
        BeanUtils.copyProperties(ExamsModelVO, ExamsEntity);
        ExamsEntity.setUpdateBy(getUsername());
        examsService.updateExamsById(ExamsEntity);
        return AjaxResult.success();
    }


    @DeleteMapping("/deleteAll")
    @ApiOperation("删除考试试卷All")
    public AjaxResult deleteAll(@RequestBody DeleteRequest request) {
        examsService.removeExamsByIds(request.getIds());
        return AjaxResult.success();
    }
}
